package cn.tedu.ivos.dictOption.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictOption.pojo.dto.SaveDictOptionDTO;
import cn.tedu.ivos.dictOption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictOption.service.DictOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/13 10:18
 */
@Slf4j
@Api(tags = "字典项模块")
@RestController
@RequestMapping("/v1/dictOption/")
public class DictOptionController {
    @Autowired
    private DictOptionService dictOptionService;

    /**
     * 查询字典项列表
     * return:JsonResult
     */
    @GetMapping("select")
    @ApiOperation("查询字典项列表")
    public JsonResult select(DictOptionQuery dictOptionQuery) {
        log.debug("查询字典项列表dictOptionQuery={}", dictOptionQuery);
        List<DictOptionVO> dictOptionList = dictOptionService.select(dictOptionQuery);
        log.debug("查询到" + dictOptionList.size() + "条字典项数据");
        return JsonResult.ok(dictOptionList);
    }

    /**
     * 保存字典项
     * return:JsonResult
     */
    @PostMapping("save")
    @ApiOperation("保存字典项")
    public JsonResult save(SaveDictOptionDTO dictOptionDTO) {
        log.debug("保存字典项dictOptionDTO={}", dictOptionDTO);
        dictOptionService.save(dictOptionDTO);
        return JsonResult.ok();
    }

    /**
     * 根据字典编码查询字典项列表
     * return:JsonResult
     */
    @GetMapping("select/{code}")
    @ApiOperation("根据字典编码查询字典项列表")
    public JsonResult selectDictOptionByCode(@PathVariable String code) {
        log.debug("根据字典编码查询字典项列表code={}", code);
        List<DictOptionVO> dictOptionList = dictOptionService.selectDictOptionByCode(code);
        log.debug("查询到" + dictOptionList.size() + "条字典项数据");
        return JsonResult.ok(dictOptionList);
    }

    /**
     * 根据dictOptionQuery查询字典项
     * return:JsonResult
     */
    @GetMapping("selectDictOption")
    @ApiOperation("根据dictOptionQuery查询字典项")
    public JsonResult selectDictOptionByCode(DictOptionQuery dictOptionQuery) {
        log.debug("根据dictOptionQuery查询字典项dictOptionQuery={}", dictOptionQuery);
        return JsonResult.ok(dictOptionService.selectDictOption(dictOptionQuery));
    }

    /**
     * 删除字典项
     * return:JsonResult
     */
    @PostMapping("delete/{id}")
    @ApiOperation("删除字典项")
    public JsonResult delete(@PathVariable Long id) {
        log.debug("删除字典项id={}", id);
        dictOptionService.delete(id);
        return JsonResult.ok();
    }
}
